import React, { forwardRef, useEffect, useImperativeHandle, useState } from 'react';

import {
  Button,
  DatePicker,
  Form,
  Input,
  Modal,
  Radio,
  Tag,
  Select,
  InputNumber,
  message,
  Row,
  Col,
} from 'antd';
import { modalLayoutLg, modalLayoutSm } from '@/config/layout';
import { request } from 'umi';
const { Option } = Select;
const DictModal = (props: any, refs: any) => {
  const [form] = Form.useForm();
  // const [fileList,setfileList] = useState([]);
  const [measureClassData, setmeasureClassData] = useState([]);
  const { setFieldsValue, resetFields } = form;

  const [code, setCode] = useState('');
  const [isModalVisible, setIsModalVisible] = useState(false);
  const [uppermenu, setuppermenu] = useState('')
  const { TextArea } = Input;
  //显示弹窗
  const showModal = () => {
    setIsModalVisible(true);
  };

  //取消
  const handleCancel = () => {
    setIsModalVisible(false);
    setCode('');
    resetFields();
    props.callback && props.callback();
  };
  //确定
  const handleOk = () => {
    form
      .validateFields()
      .then((vals) => {
        let Obj = form.getFieldsValue();
        Obj = {
          ...Obj,
          ...{
            supMenu: uppermenu
          }
        };

        const url = code ? '/sys/menu/' : '/sys/menu/';
        const method = code ? 'PUT' : 'POST';
        if (code) {
          Obj.menuId = code;
        } else {
        }
        request(url, {
          method: method,
          headers: {
            'Content-Type': 'application/json',
          },
          data: Obj,
        }).then((res) => {
          message.success(res?.meta?.msg);
          setIsModalVisible(false);
          handleCancel();
        });
      })
      .catch((info) => { });
  };

  const getDetail = (id: any) => {
    request('/sys/menu/' + id, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
      data: {
        id,
      },
    }).then((res) => {
      setFieldsValue(res?.data);
      showModal();
    });
  };
  useImperativeHandle(refs, () => ({
    init: (id: any, uppermenun: any) => {

      setuppermenu(uppermenun)
      if (id) {
        setCode(id);
        getDetail(id);
      } else {
        resetFields();
        showModal();
      }

      // setfileList([]);
    },
  }));

  return (
    <Modal
      title={code === '' ? '新增' : '编辑'}
      visible={isModalVisible}
      onOk={handleOk}
      onCancel={handleCancel}
      centered
      width={1000}
      maskClosable={false}
      bodyStyle={{
        maxHeight: window.innerHeight - 150,
        overflowY: 'auto',
      }}
    >
      <Form form={form} layout="vertical" {...modalLayoutSm}>
        <Row>
          <Col span={12}>
            <Form.Item
              label="菜单名称"
              name="menuName"
              rules={[{ required: true, message: '请输入菜单名称!' }]}
            >
              <Input />
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              label="菜单key"
              name="menuKey"
              rules={[{ required: true, message: '请输入菜单key' }]}
            >
              <Input />
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              label="菜单路径"
              name="menuPath"
              rules={[{ required: true, message: '请输入菜单路径!' }]}
            >
              <Input />
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item label="菜单类型" name="sourceType">
              {/* <InputNumber style={{ width: '100%' }} /> */}
              <Select>
                <Option value="WEB">WEB</Option>
                <Option value="APP">APP</Option>
              </Select>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item label="排序" name="sno">
              {/* <InputNumber style={{ width: '100%' }} /> */}
              <Input />
            </Form.Item>
          </Col>

        </Row>
      </Form>
    </Modal>
  );
};
const AddMCom = forwardRef(DictModal);
export default AddMCom;
